﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Services.Tokens;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.UI.WebControls;
using DotNetNuke.Modules.Events.ScheduleControl.MonthControl;

using CharityGamesOnline.Modules.Common;

namespace CharityGamesOnline.Modules.Event
{
    public partial class EventMonth : CGOBase
    {

        private List<EventInfo> lstEvent;
        private DateTime dateSelected;
        private const int DEFAULT_EVENT_NAME_LENGTH = 15;
        private const string DEFAULT_OVER_LENGTH_FORMET_STR = "{0}...";

        public int EventDetailTabID
        {
            get
            {
                if (!Null.IsNull(Settings["EventDetailTabID"]))
                {
                    return Int32.Parse(Settings["EventDetailTabID"].ToString());
                }
                else
                    return TabId;
            }
        }

        #region Event Handler

        protected void Page_Load(object sender, EventArgs e)
        {
            //dnnEventCalendar.SelectionMode = CalendarSelectionMode.None;
            //dnnEventCalendar.NextPrevFormat = NextPrevFormat.FullMonth;
            //dnnEventCalendar.VisibleDate                        
            if (!Page.IsPostBack)
            {
                if (Null.IsNull(dateSelected))
                {
                    dateSelected = DateTime.Now.Date;
                }
                dnnEventCalendar.TodaysDate = DateTime.Now.Date;
                dnnEventCalendar.SelectedDate = dateSelected;
                dnnEventCalendar.VisibleDate = dateSelected;
                BindData();
            }
                        
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            dnnEventCalendar.SelectionChanged+=new EventHandler(dnnEventCalendar_SelectionChanged);
            dnnEventCalendar.VisibleMonthChanged+=new MonthChangedEventHandler(dnnEventCalendar_VisibleMonthChanged);
            dnnEventCalendar.DayRender+=new DayRenderEventHandler(dnnEventCalendar_DayRender);
        }

        void dnnEventCalendar_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
        {
            dateSelected = e.NewDate;
            dnnEventCalendar.SelectedDate = dateSelected;
            BindData();
        }

        void dnnEventCalendar_SelectionChanged(object sender, EventArgs e)
        {
            dnnEventCalendar.VisibleDate = dnnEventCalendar.SelectedDate;
            dateSelected = dnnEventCalendar.SelectedDate.Date;
            BindData();
        }

        void dnnEventCalendar_DayRender(object sender, DayRenderEventArgs e)
        {
            //throw new NotImplementedException();
            Literal litCell = new Literal();
            List<EventInfo> lstDisplayEvent = GetDateEvents(lstEvent, e.Day.Date);            

            //if (lstDisplayEvent.Count > 0)
            //{
            //    e.Day.IsSelectable = true;
            //    //set different style for cell with event
            //    if (e.Day.Date == dateSelected.Date)
            //    {
            //        e.Cell.CssClass = "EventSelectedDay";
            //    }
            //    else
            //    {
            //        if (e.Day.IsWeekend)
            //            e.Cell.CssClass = "EventWeekendDayEvents";
            //        else
            //            e.Cell.CssClass = "EventDayEvents";
            //    }                
                
            //}
            //else
            //{
            //    e.Day.IsSelectable = false;
            //}

            string strCellData = string.Empty;

            foreach (EventInfo objEvent in lstDisplayEvent)
            {
                HyperLink lnkDaily = new HyperLink();
                if (objEvent.Name.Length > DEFAULT_EVENT_NAME_LENGTH)
                {
                    lnkDaily.Text = string.Format(DEFAULT_OVER_LENGTH_FORMET_STR,objEvent.Name.Substring(0, DEFAULT_EVENT_NAME_LENGTH));
                }
                else
                    lnkDaily.Text = objEvent.Name;
                if (EventDetailTabID == TabId || EventDetailTabID < 0)
                {
                    lnkDaily.NavigateUrl = Globals.NavigateURL(EventDetailTabID, string.Empty, "mid=" + ModuleId.ToString(), "mctl=EventDetail", "EventID=" + objEvent.EventID);
                }
                else
                {
                    lnkDaily.NavigateUrl = Globals.NavigateURL(EventDetailTabID, string.Empty, "EventID=" + objEvent.EventID);
                }
                //lnkDaily.Style.Add("cursor", "hand");
                //lnkDaily.Attributes.Add("onclick", "javascript:return false;");

                System.IO.StringWriter strWriter = new System.IO.StringWriter();
                HtmlTextWriter htmlWriter = new HtmlTextWriter(strWriter);
                lnkDaily.RenderControl(htmlWriter);
                strCellData = strCellData + "<div>" + strWriter.ToString() + "</div>";
            }

            litCell.Text = "<div class='EventDayScroll'>" + strCellData + "</div>";
            e.Cell.Controls.Add(litCell);
        }
        #endregion

        #region Private Function
        private void BindData()
        {
            DateTime dateInit, dateBegin, dateEnd;
            EventController ctrlEvent = new EventController();
            dateInit = new DateTime(dateSelected.Year, dateSelected.Month, 1);
            dateBegin = dateInit.AddDays(-10);
            dateEnd = dateInit.AddMonths(1).AddDays(10);
            lstEvent = ctrlEvent.GetEvents(PortalId, 0, dateBegin, dateEnd);
        }

        private List<EventInfo> GetDateEvents(List<EventInfo> lstEvent, DateTime SelectDate)
        {
            List<EventInfo> retEventList = new List<EventInfo>();
            foreach (EventInfo objEvent in lstEvent)
            {
                if (objEvent.DateBegin.Date <= SelectDate && objEvent.DateEnd.Date >= SelectDate)
                    retEventList.Add(objEvent);
            }
            return retEventList;
        }
        
        #endregion
    }
}